const Koa = require('koa2');
const router = require('koa-router')();
const Cache = require('./cache/cache');
const request=require('request');
const app = new Koa();
const port = 3002;
app.use(new Cache())
router.get('/api/data/:id', async (ctx,next) => {
  ctx.body =ctx;
});

app
  .use(router.routes())
  .use(router.allowedMethods());
app.listen(port, () => {
  console.log(`启动服务，http://localhost:${port}`);
});

//测试缓存接口
function testUnit(count) {
  if (count < 1) return;
  //每次获取一个10以内的随机数进行测试
  let num = Math.floor(Math.random() * 10);
  request(`http://localhost:3002/api/data/${num}`, {encoding: null}, () => {});
  setTimeout(() => {
    testUnit(count - 1);
  }, 500);
}

testUnit(20);//执行次数
